package cn.biplam.front.component.view.systemManage.task.modelimport;

import java.util.ArrayList;

import cn.biplam.common.runtime.Task;
import cn.biplam.common.runtime.TaskBase;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.data.common.ButtonComp;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Length_Unit;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.PopupScript;
import cn.biplam.front.htmlscript.script.AjxScript.Level;
import cn.biplam.front.template.base.TextArea;
import cn.biplam.front.template.base.Button.Button_Type;
import cn.biplam.front.template.control.FlatTable;

/**
 * 显示模型导入的进行信息
 * @author WangSong 2009-7-25下午02:40:58
 * @version 1.0
 */
public class ModelImportProcess {
	
	private String ID;
	private UserInfo mUser;
	private boolean buttonShow = false;
	
	/**
	 * 构造方法
	 * @param ID   标识ID
	 * @param mUser 用户信息
	 */
	public ModelImportProcess(String ID,UserInfo mUser){
		this.ID = ID+"process";
		this.mUser = mUser;
	}
	
	/**
	 * 取导入任务
	 * @param taskId
	 * @return
	 * @throws ErrorMessage
	 */
	public HtmlWindow get(String taskId) throws ErrorMessage{
		HtmlWindow process = new HtmlDiv("myRefresh");
		String msg = getPrompt(taskId);
		FlatTable ft=new FlatTable(ID,"");
		/**Select名字*/
		ft.putTh("导入信息", 0, 0);
		
		ft.putTd(getButton(),0,1);
		ft.setColWidth(0, "50",Length_Unit.percent);
		process.put(ft.get());
		process.put(getShow(msg));
		process.setStyleWidth("610");
		process.setStyleHeight("400");
		if(!buttonShow){
			process.bind(refresh(taskId));
		}
		return process;
	}
	
	/**
	 * 导入内容显示文本框
	 * @param content  显示的字符串
	 * @return 导入进行程度信息
	 * @throws ErrorMessage
	 */
	private HtmlWindow getShow(String content) throws ErrorMessage{
		TextArea t = new TextArea(null,"show",content,93,22);
		return t.get();
	}
	
	/**
	 * 根据任务ID取任务状态信息
	 * @param taskId 任务ID
	 * @return 导入状态提示信息
	 * @throws ErrorMessage
	 */
	private String getPrompt(String taskId) throws ErrorMessage{
		StringBuilder sb = new StringBuilder();
		TaskBase tb = Task.getTask("Buffer2MD:"+taskId);
		if(tb!=null){
			ArrayList<String> status = tb.getStatusPrompt();
			for(int i=status.size();i>1;i--){
				if("导入完成".equals(status.get(i-1)) || "导入异常".equals(status.get(i-1))){
					buttonShow = true;
				}
				sb.append(status.get(i-1));
				sb.append("\n");
			}
		}else{
			buttonShow = true;
			sb.append("导入完成!");
		}
		return sb.toString();
	}
	
	/**
	 * 返回按钮，并刷导入任务管理页面
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow getButton() throws ErrorMessage{
		HtmlWindow button = null;
		if(buttonShow){
			button = ButtonComp.get(mUser,"259",Button_Type.Click);
			button.bind(Window_Event.OnClick,PopupScript.cancel("syspop"+Level.Third)+
					AjxScript.getA(ModelImportTaskManage.cId,ModelImportTaskManage.aId_Main_Page,"refresh"));
			
		}else{
			button = ButtonComp.get(mUser,"259",Button_Type.ReadOnly);
		}
		
		return button;
	}
	
	/**
	 * 刷新脚本
	 * @param taskId 任务ID
	 * @return 刷新脚本字符串
	 */
	private String refresh(String taskId){
		StringBuilder sb = new StringBuilder();
		sb.append("if(true){");
		sb.append(" setTimeout(\"a('myRefresh','"+ModelImportTaskManage.aId_Import_TO_Md_Process+"','"+taskId+"')\",5000);");
		sb.append("};");		
		return sb.toString();
	}
	
}
